﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlTypes;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DataGridViewCellExtLib;

namespace WindowsFormsApp
{
    public partial class MainForm : Form
    {
        /// <summary>
        /// 操作列单元格索引值
        /// </summary>
        private const int _cellIndex = 3;
        /// <summary>
        /// “-”按钮对象
        /// </summary>
        private Button _subBtn;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            InitDataGridView();
        }

        /// <summary>
        /// 初始化DataGridView
        /// </summary>
        private void InitDataGridView()
        {
            dataGridView.Rows.Add();
            var cell = dataGridView.Rows[0].Cells[_cellIndex];
            AddPanelConrol(cell);
        }

        /// <summary>
        /// 添加Panel控件
        /// </summary>
        /// <param name="cell"></param>
        private void AddPanelConrol(DataGridViewCell cell)
        {
            var panel = cell.AddPanelControl(dataGridView);
            panel.BackColor = Color.Transparent;

            //添加Button
            int y = 3;
            int btnWidth = 100;
            int btnHeight = panel.Height - y * 2;
            int x1 = 40;
            int x2 = panel.Width - x1 - btnWidth;

            var addBtn = new Button
            { 
                Name = "addBtn",
                Text = "+",
                Font = new Font("隶书", 15f),
                Location = new Point(x1, y),
                Size = new Size(btnWidth, btnHeight),
            };
            addBtn.Click += AddRowEvent;

            var subBtn = new Button
            { 
                Name = "subBtn",
                Text = "-",
                Font = new Font("隶书", 15f),
                Location = new Point(x2, y),
                Size = new Size(btnWidth, btnHeight),
                //如果DataGridView只有一行, subBtn的Enable属性设置为false
                Enabled = dataGridView.RowCount > 1
            };
            subBtn.Click += RemoveRowEvent;

            //如果是首行的“-”按钮则赋值
            if (cell.RowIndex == 0)
            {
                _subBtn = subBtn;
            }

            panel.Controls.AddRange(new Control[] { addBtn, subBtn });
        }

        /// <summary>
        /// 添加行的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddRowEvent(object sender, EventArgs e)
        {
            int rowIndex = dataGridView.Rows.Add();
            var cell = dataGridView.Rows[rowIndex].Cells[_cellIndex];
            AddPanelConrol(cell);

            //设置首行“-”按钮的Enable属性
            if (_subBtn != null)
            {
                _subBtn.Enabled = dataGridView.RowCount > 1;
            }
        }

        /// <summary>
        /// 删除行的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RemoveRowEvent(object sender, EventArgs e)
        {
            var btn = (Button)sender;
            var panel = (Panel)btn.Parent;
            var bindingTag = (TagPlus<DataGridViewCell>)panel.Tag;
            var cell = bindingTag.Binding;
            cell.RemovePanelControl(dataGridView);

            //如果要删除的行索引值是0，则重新设置首行中“-”按钮对象
            if (cell.RowIndex == 0)
            {
                //获取下一行的“-”按钮对象
                int nextRowIndex = cell.RowIndex + 1;
                var nextCell = dataGridView.Rows[nextRowIndex].Cells[_cellIndex];
                var nextPanel = ((TagPlus<Panel>)nextCell.Tag).Binding;
                foreach (Control subControl in nextPanel.Controls)
                {
                    if (subControl is Button subBtn && subBtn.Name == "subBtn")
                    {
                        _subBtn = subBtn;
                        break;
                    }
                }
            }

            //移除前先保存行索引, 因为移除后行索引会变为-1
            int rowIndex = cell.RowIndex;
            //移除当前行
            dataGridView.Rows.RemoveAt(cell.RowIndex);

            //重新设置Panel控件位置
            for (int i = rowIndex; i < dataGridView.RowCount; i++)
            {
                var cellItem = dataGridView.Rows[i].Cells[_cellIndex];
                cellItem.ResetPanelControlLocation(dataGridView);
            }

            //设置首行“-”按钮的Enable属性            
            if (dataGridView.RowCount == 1)
            {
                if (_subBtn != null)
                {
                    _subBtn.Enabled = dataGridView.RowCount > 1;
                }
            }
        }
    }
}
